//  @@@ web_export_view custom JS @@@
//#############################################################################
//   Copyright (C) 2016 开阖有限公司 (<http://www.osbzr.com>)(学习原oca模块 web_export_view)
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero General Public License as published
//    by the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//#############################################################################
odoo.define('web_export_view_good.web_export_view_good', function (require) {
"use strict";
    var ListView = require('web.ListController');
    var core = require('web.core');
    var session = require('web.session');

    var QWeb = core.qweb;
    ListView.include({
        init: function () {
            this._super.apply(this, arguments);
        },
        renderButtons: function ($node) {
            var self = this;
            var add_button = false;
            if (!this.$buttons) { // Ensures that this is only done once
                add_button = true;
            }
            self._super.apply(self, arguments); // Sets this.$buttons
            self.$buttons.on('click', '.o_button_export', self.button_export_action.bind(self));
        },
        compute_main_data: function (rows, export_columns_keys) {
            var export_rows = []
            $.each(rows, function () {
                var $row = $(this);
                if ($row.find('.o_data_cell').length != 0) {
                    var export_row = [];
                    $.each($row.find('> td '), function (index, element) {
                         var cell = $(element);
                        var text = cell.text() || cell.textContent || cell.innerHTML || "";
                        if (export_columns_keys[index]) {
                            export_row.push(text);
                        }
                    });
                    if(export_row){
                        export_rows.push(export_row);
                    }
                }
            });
            return export_rows;
        },
        button_export_action: function () {
            var self = this;
            var view = self;
            var export_columns_keys = [];
            var export_columns_names = [];
            var location = 0;
            var export_rows = []
            $.each(view.$el.find('.o_list_view > thead > tr > th'), function (index, element) {
                var cell = $(element);
                var text =  (cell.text().trim()).replace(/[\x00-\x20\x7F-\xA0\u1680\u180E\u2000-\u200B\u2028\u2029\u202F\u205F\u3000\uFEFF]/g, '');
                // 找到 查看明细 按钮所在的位置
                if (text.length === 0) {
                    export_columns_keys.push(false);
                }else{
                    export_columns_names.push(text);
                    export_columns_keys.push(true);
                }
            });
            export_rows.push(export_columns_names);
            var rows = view.$el.find('.o_list_view > tbody > tr');
            export_rows = export_rows.concat(this.compute_main_data(rows, export_columns_keys));
            // 排除掉 查看明细 按钮所在的列
            if (location != 0) {
                for (var i = 0; i < export_rows.length; i++) {
                    export_rows[i].splice(location - 1, 1);
                }
            }
            this._rpc({
                model: 'report.template',
                method: 'get_time',
                args: [this.modelName],
                kwargs: {},
            }).then(function (data) {
                var now_day = data[0];
                var operation_message = new Array(export_columns_names.length);
                var header = new Array(export_columns_names.length);
                for (var i = 0; i < operation_message.length; i++) {
                    operation_message[i] = " ";
                }
                operation_message[0] = "操作人";
                operation_message[1] = session.username;
                operation_message[operation_message.length - 2] = "操作时间";
                operation_message[operation_message.length - 1] = now_day.trim();
                export_rows.push(operation_message);
                for (var i = 0; i < data[3]; i++) {
                    export_rows.splice(i, 0, []);
                }
                for (var i = 0; i < data[2]; i++) {
                    export_rows.splice(i, 0, []);
                }
                header[0] = view.displayName
                $.blockUI();
                session.get_file({
                    url: '/web/export/export_xls_view',
                    data: {
                        data: JSON.stringify({
                            model: view.modelName,
                            headers: header,
                            files_name: view.displayName,
                            rows: export_rows,
                            file_address: data[1]
                        })
                    }, complete: $.unblockUI
                });
            });
        }
    });
});